<?php namespace Model;
    class userModel {
        private $DB;
        protected $PHONE;
        protected $EMAIL;
        protected $ID;

        public function __construct() {
            $this->DB = mysqli_connect('ppt_mysql', 'root', 'root', 'ppt');
        }

        public function addUser($phone, $email = null) {
            $regexp = '/^\s?(\+\s?7|8)([- ()]*\d){10}$/';
            $data = date("Y/m/d");
            $sql = "";

            if (!(preg_match($regexp, $phone)) && !preg_replace("/[^0-9]/", '', $phone)) {
                return false;
            }
            if ($phone[0] == "8") {
                $phone[0] = "7";
            }
            elseif ($phone[0] == "+") {
                str_replace("+", "", $phone);
            }

            $sql = "select * from users where phone = $phone";
            $response = $this->DB->query($sql)->fetch_assoc();
            $token = md5($phone);

            if ($response !== null) {
                return [
                    "user" => $response,
                    "phone" => $phone,
                    "status" => "update"
                ];
            } else {
                $sql = "INSERT INTO users (phone, email, created, updated, token)
                            VALUES
                            ('$phone', '$email', '$data', '$data', '$token')";
                $this->DB->query($sql);
                $response = $this->DB->query("select * from users where phone = $phone");
                if ($response != null) {
                    return [
                        "user" => $response->fetch_assoc(),
                        "phone" => $phone,
                        "status" => "create"
                    ];
                } else {
                    return "Exeption";
                }
            }
        }

        public function getUserBy($id = null, $phone = null, $email = null) {
            $sql = "select * from `users` where ";
            $response = "";

            $obj = [
                "id" => $id,
                "phone" => $phone,
                "email" => $email
            ];

            foreach ($obj as $key => $item) {
                if ($item !== null) {
                    $sql = $sql."$key = '$item'";
                } else {
                    continue;
                }
            }

            $response = $this->DB->query($sql);
            $response = $response->fetch_assoc();

            return $response;
        }

        public function test() {
            $sql = "select * from `users` where phone = '89990000000'";
            $response = mysqli_query($this->DB, $sql);

            return $response;
        }

        public function getAllUsers() {
            $sql = "SELECT * FROM users";
            $response = $this->DB->query($sql)->fetch_array();
            return $response;
        }

        public function checkUserToken($token) {
            $sql = "select * from users where token = '$token'";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response != null) {
                return $response;
            } else {
                return "Permission denied";
            }
        }

        public function deleteUser() {

        }

    }